#include "man_100.h"
#include "ui_man_100.h"
#include<QPainter>
#include<Qtimer>
#include<QKeyEvent>
#include<QPen>
man_100::man_100(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::man_100),Start(false),direction(-1),first(0),first1(0),first2(0),first3(0),firstWind(0),count(1),Board1start(0),Board2start(0),Board3start(0),Windstart(0),OnBoard(0),OnBoard1(0),OnBoard2(0),OnBoard3(0),OnWind(0),speed(300)
{
    ui->setupUi(this);
    this->setGeometry(QRect(800,380,400,400));
}

man_100::~man_100()
{
    delete ui;
}
void man_100::paintEvent(QPaintEvent *event){//绘画事件
    if(Start == false){//Man()构造函数只执行一次
        Man();
    }
    QPainter painter(this);
    if(gameover == true){//游戏结束则计时器停止
        timer->stop();
    }
    //画游戏背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,15,380,380);
    painter.drawPixmap(10,15,380,380,QPixmap(":/Background/background.jpg"));
    //画球
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawEllipse(man);
    //画板、板1、板2、板3、风口
    painter.setPen(Qt::gray);
    painter.setBrush(Qt::gray);
    painter.drawRect(Board);
    painter.drawPixmap(Board,QPixmap(":/Background/board1.jfif"));
    if(Board1start){
        painter.drawRect(Board1);
        painter.drawPixmap(Board1,QPixmap(":/Background/board0.jfif"));
    }
    if(Board2start){
        painter.drawRect(Board2);
        painter.drawPixmap(Board2,QPixmap(":/Background/board0.ifjf"));
    }
    if(Board3start){
        painter.drawRect(Board3);
        painter.drawPixmap(Board3,QPixmap(":/Background/board.ifjf"));
    }
    if(Windstart){
        painter.setPen(Qt::white);
        painter.setBrush(Qt::white);
        painter.drawRect(Wind);
    }
    //显示游戏结束与得分图标
    QFont font1("Courier",18);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(10,120,GAME);
    if(gameover == true){
        painter.drawText(10,150,"您的得分是：");
        painter.drawText(160,150,QString::number(Score));
    }
}
void man_100::Man(){//人的初始化与活动
    Start=true;//标志游戏开始
    gameover = false;//游戏结束
    Score = 0;//得分
    GAME = "";
    QRect rect(200,10,10,10);//初始化球的位置
    man=rect;
    timer = new QTimer(this);//计时器
    timer->start(speed);//初始值300ms，之后每次减少30ms，直至120ms停止递减
    connect(timer,SIGNAL(timeout()),SLOT(man_update()));
    Board = board();//板的构造，后面几个类似
    Board1 = board1();
    Board2 = board2();
    Board3 = board3();
    Wind = wind();
    Wind.setLeft(Wind.left()-200);//将风口移动至游戏画面外
    Wind.setRight(Wind.right()-200);
}
void man_100::man_update(){//人的位置更新，板的位置更新，风口的产生与消失
    if(Board.top() == 10){//板重新生成的条件
        Board=board();
    }
    if(Board1.top() == 10){
        Board1 = board1();
    }
    if(Board2.top() == 10){
        Board2 = board2();
    }
    if(Board3.top() == 10){
        Board3 = board3();
    }
    if(count-when_the_Wind_arises>5){//将风口移动到画面外并且不画出来，这样的话在画面中就看不见风口了
        Windstart = 0;
        firstWind = 0;
        OnWind = 0;
        Wind = wind();
        Wind.setLeft(Wind.left()-200);
        Wind.setRight(Wind.right()-200);
    }
    if(count%23 == 0){//何时第一次生成板的判定
        Board1start = 1;
    }
    if(count%45 == 0){//何时第二次生成板的判定
        Board2start = 1;
    }
    if(count%103 == 0){//何时第三次生成板的判定
        Board3start = 1;
    }
    int where[4];//部分是对四个板的位置关系进行判定，如果他们相邻两者之间的最大距离超过180，则在底部中央附近生成风口
    where[0] = Board.left(),where[1] = Board1.left(),where[2] = Board2.left(),where[3] = Board3.left();
    for(int i = 0;i < 3;i++){
        for(int j = 3;j > i;j--){
            if(where[j] < where[j-1]){
                int temp = where[i];
                where[i] = where[j];
                where[j] = temp;
            }
        }
    }
    if((where[3]-where[2]>180)||(where[2]-where[1]>180)||(where[1]-where[0]>180)){
        Windstart = 1;
        Wind = wind();
    }
    if(man.top()+10==Board.top()&&man.left()<Board.right()&&man.right()>Board.left()){//判断球是否在板上
        OnBoard = 1;
        if(first == 0){
            direction = 4;
            first = 1;
        }
    }
    if(man.top()+10==Board1.top()&&man.left()<Board1.right()&&man.right()>Board1.left()){//判断球是否在板1上
        OnBoard1 = 1;
        if(first1 == 0){
            direction = 4;
            first1 = 1;
        }
    }
    if(man.top()+10==Board2.top()&&man.left()<Board2.right()&&man.right()>Board2.left()){//判断球是否在板2上
        OnBoard2 = 1;
        if(first2 == 0){
            direction = 4;
            first2 = 1;
        }
    }
    if(man.top()+10==Board3.top()&&man.left()<Board3.right()&&man.right()>Board3.left()){//判断球是否在板3上
        OnBoard3 = 1;
        if(first3 == 0){
            direction = 4;
            first3 = 1;
        }
    }
    if(man.top()+10==Wind.top()&&man.left()<Wind.right()&&man.right()>Wind.left()){//判断球是否在风口上
        OnWind = 1;
        when_the_Wind_arises = count;
        if(firstWind == 0){
            direction = 4;
            firstWind = 1;
        }
    }
    if(count%50 == 0){//没隔一定时间，加速一次
        if(speed > 120){
            speed -= 9;
        }
        else if(first == 1||first1==1||first2 == 1||first3 == 1){//加速到一定限度后，再次落在任何一个板上，结束游戏
            GAME = "恭喜您，坚持到最后，探险成功了！";
            gameover = true;
        }
        timer->stop();
        timer->start(speed);

    }
    switch(direction){//对移动类型分类，分别为，不在木块上的左、右移动，在木块上的左、右移动以及踩在木块上后如果不按左右键将随木块一同上升
    case 0://不在任何一个板或者风口上的左移
        man.setLeft(man.left()-5);
        man.setRight(man.right()-5);
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        break;
    case 1://不在任何一个板或者风口上的右移
        man.setLeft(man.left()+5);
        man.setRight(man.right()+5);
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        break;
    case 2://在板上的左移，有可能离开这个板，有可能还在这个板上，同时对在板、板1等上作了分类
        man.setLeft(man.left()-5);
        man.setRight(man.right()-5);
        if(OnBoard == 1&&man.right()<=Board.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            OnBoard = 0;
            first = 0;
            Score++;
        }
        else if(OnBoard1 == 1&&man.right()<=Board1.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            OnBoard1 = 0;
            first1 = 0;
            Score++;
        }
        else if(OnBoard2 == 1&&man.right()<=Board2.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            OnBoard2 = 0;
            first2 = 0;
            Score++;
        }
        else if(OnBoard3 == 1&&man.right()<=Board3.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            first3 = 0;
            Score++;
            OnBoard3 = 0;
        }
        else if(OnWind == 1&&man.right()<=Wind.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            OnWind = 0;
            firstWind = 0;
        }
        else{
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            first = 0;
            first1 = 0;
            first2 = 0;
            firstWind = 0;
        }
        break;
    case 3://在板上的右移，有可能离开这个板，有可能还在这个板上，同时对在板、板1等上作了分类
        man.setLeft(man.left()+5);
        man.setRight(man.right()+5);
        if(OnBoard == 1&&man.left()>=Board.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            OnBoard = 0;
            Score++;
            first = 0;
        }
        else if(OnBoard1 == 1&&man.left()>=Board1.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            first1 = 0;
            OnBoard1 = 0;
            Score++;
        }
        else if(OnBoard2 == 1&&man.left()>=Board2.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            first2 = 0;
            OnBoard2 = 0;
            Score++;
        }
        else if(OnBoard3 == 1&&man.left()>=Board3.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            first3 = 0;
            OnBoard3 = 0;
            Score++;
        }
        else if(OnWind == 1&&man.left()>=Wind.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            firstWind = 0;
            OnWind = 0;
        }
        else{
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            first = 0;
            first1 = 0;
            first2 = 0;
            firstWind = 0;
        }
        break;
    case 4://落在板或风口上后不按键的情况，与板一同上升或被风口吹上去
        man.setTop(man.top()-5);
        man.setBottom(man.bottom()-5);
        break;
    default://直接下落
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        break;
    }
    Board.setTop(Board.top()-5);//下面一段都是板的向上移动
    Board.setBottom(Board.bottom()-5);
    if(Board1start == 1){
        Board1.setTop(Board1.top()-5);
        Board1.setBottom(Board1.bottom()-5);
    }
    if(Board2start == 1){
        Board2.setTop(Board2.top()-5);
        Board2.setBottom(Board2.bottom()-5);
    }
    if(Board3start == 1){
        Board3.setTop(Board3.top()-5);
        Board3.setBottom(Board3.bottom()-5);
    }
    if(man.top()<15||man.bottom()>390||man.left()<10||man.right()>390){
        gameover = true;
        GAME = "Game Over!";
    }
    count++;
    update();//更新
}
void man_100::keyPressEvent(QKeyEvent *event){//人的键盘事件
    QKeyEvent *key=(QKeyEvent*) event;
    if((man.top()+10==Board.top()&&man.left()<Board.right()&&man.right()>Board.left())||(man.top()+10==Board1.top()&&man.left()<Board1.right()&&man.right()>Board1.left())||(man.top()+10==Board2.top()&&man.left()<Board2.right()&&man.right()>Board2.left())||(man.top()+10==Board3.top()&&man.left()<Board3.right()&&man.right()>Board3.left())){
        switch(key->key()){
        case Qt::Key_Left:
            direction = 2;
            break;
        case Qt::Key_Right:
            direction = 3;
            break;
        }
    }
    else{
        switch(key->key()){
        case Qt::Key_Left:
            direction = 0;
            break;
        case Qt::Key_Right:
            direction = 1;
            break;
        }
    }
}
QRect man_100::board(){//构造函数
    int x;
    x = qrand()%36;
    QRect RECT(10+x*10,390,15,5);
    return RECT;
}
QRect man_100::board1(){
    int x;
    x = qrand()%38;
    QRect RECT(10+x*10,390,5,5);
    return RECT;
}
QRect man_100::board2(){
    int x;
    x = qrand()%36;
    QRect RECT(10+x*10,390,5,5);
    return RECT;
}
QRect man_100::board3(){
    int x;
    x = qrand()%36;
    QRect RECT(10+x*10,390,20,5);
    return RECT;
}
QRect man_100::wind(){
    QRect RECT(200,390,10,5);
    return RECT;
}
